﻿using MiniJob.Service;

namespace MiniJob.Orleans;

public interface IAppGrain : IGrainWithGuidKey
{
    Task ScheduleAppAsync();
}

public class AppGrain : GrainBase, IAppGrain
{
    public async Task ScheduleAppAsync()
    {
        var jobService = ServiceProvider.GetRequiredService<IJobService>();
        var appId = this.GetPrimaryKey();

        var nowTime = DateTime.Now.ToUnixTimestampByMilliseconds();
        var timeThreshold = nowTime + 2 * 15000;
        var jobs = await jobService.FindTriggerJobs(appId, timeThreshold);
        foreach (var job in jobs)
        {
            await GrainFactory.GetGrain<IJobGrain>(job.Id).ScheduleJobAsync();
        }
    }
}
